package com.zx.kitchen.index.common.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bumptech.glide.Glide;
import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.entity.SupplierBean;
import com.zx.kitchen.index.common.http.HttpIndex;
import com.zx.kitchen.index.common.mvp.OnResultListener;


/**
 * Created by Administrator on 2017/3/13.
 */

public class BusinessInfoActivity extends Activity implements View.OnClickListener {

    private ImageView imgLogo;
    private ImageView imgQrcode;
    private TextView tvBusinessName;

    private TextView etPhone;
    private TextView etTelphone;
    private TextView etAddress;
    private TextView etBusinessDetail;
    private Spinner spDay;
    private Spinner spMonth;
    private TextView etBusinessActivity;
    private RadioGroup rgPrintInfo;
    private RadioButton rbYes;
    private RadioButton rbNo;
    private ImageView toolbarLeft;
    private TextView toolbarTitle;
    private static String[] mFontArrayStringDay = new String[]{};
    private static String[] mFontArrayStringMonth = new String[]{};
    private ArrayAdapter<String> mArrayAdapter;
    private int mSpDay = 1;
    private int mSpMonth = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_info);

        initView();
        initData();
    }

    private void initData() {

        HttpIndex.getBusinessInfo(getBaseContext(), new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                SupplierBean supplierBean = JSON.parseObject(str, new TypeReference<SupplierBean>() {
                });
                Glide.with(getBaseContext()).load(supplierBean.getLogo()).into(imgLogo);
                Glide.with(getBaseContext()).load(supplierBean.getQr_code()).into(imgQrcode);
                tvBusinessName.setText(supplierBean.getName());
                etPhone.setText(supplierBean.getMobile());
                etTelphone.setText(supplierBean.getCompanytel());
                etAddress.setText(supplierBean.getCompanyaddress());
                etBusinessDetail.setText(supplierBean.getCompanyaddress());
                etBusinessActivity.setText(supplierBean.getActivity());
                if (supplierBean.getActivity_print().equals("1")) {
                    rbYes.setChecked(true);
                } else {
                    rbNo.setChecked(true);
                }

            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        }, null);
    }

    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        imgLogo = (ImageView) findViewById(R.id.img_logo);
        imgQrcode = (ImageView) findViewById(R.id.img_qrcode);
        tvBusinessName = (TextView) findViewById(R.id.tv_business_name);

        etPhone = (TextView) findViewById(R.id.et_phone);
        etTelphone = (TextView) findViewById(R.id.et_telphone);
        etAddress = (TextView) findViewById(R.id.et_address);
        etBusinessDetail = (TextView) findViewById(R.id.et_business_detail);
        spDay = (Spinner) findViewById(R.id.sp_day);
        spMonth = (Spinner) findViewById(R.id.sp_month);
        etBusinessActivity = (TextView) findViewById(R.id.et_business_activity);
        rgPrintInfo = (RadioGroup) findViewById(R.id.rg_print_info);
        rbYes = (RadioButton) findViewById(R.id.rb_yes);
        rbNo = (RadioButton) findViewById(R.id.rb_no);

        toolbarTitle.setText(getResources().getString(R.string.baseInfoSetting));
        toolbarTitle.setTextColor(getResources().getColor(R.color.blue));

        findViewById(R.id.btn_update).setOnClickListener(this);
        toolbarTitle.setOnClickListener(this);

        mFontArrayStringDay = new String[24];
        mFontArrayStringMonth = new String[29];

        for (int i = 0; i < mFontArrayStringDay.length; i++) {
            mFontArrayStringDay[i] = i + ":00";
        }
        for (int i = 0; i < mFontArrayStringMonth.length; i++) {
            mFontArrayStringMonth[i] = i + "日";
        }

        //        seeting_spinner改变了spinner的默认样式
        mArrayAdapter = new ArrayAdapter<String>(this, R.layout.edit_spinner, mFontArrayStringDay) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
//                    设置spinner展开的Item布局
                    convertView = getLayoutInflater().inflate(R.layout.edit_spinner_item, parent, false);
                }
                TextView spinnerText = (TextView) convertView.findViewById(R.id.spinner_textView);
                spinnerText.setText(getItem(position));
                return convertView;
            }
        };
        //        spinner设置监听
        spDay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mSpDay = Integer.parseInt(mFontArrayStringDay[position].replace(":00", ""));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spDay.setAdapter(mArrayAdapter);
        //        seeting_spinner改变了spinner的默认样式
        mArrayAdapter = new ArrayAdapter<String>(this, R.layout.edit_spinner, mFontArrayStringMonth) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
//                    设置spinner展开的Item布局
                    convertView = getLayoutInflater().inflate(R.layout.edit_spinner_item, parent, false);
                }
                TextView spinnerText = (TextView) convertView.findViewById(R.id.spinner_textView);
                spinnerText.setText(getItem(position));
                return convertView;
            }
        };
        spMonth.setAdapter(mArrayAdapter);
        //        spinner设置监听
        spMonth.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mSpMonth = Integer.parseInt(mFontArrayStringMonth[position].replace("日", ""));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_update:
                startActivity(new Intent(this, EditBusinessInfoActivity.class));
                break;
            case R.id.toolbar_title:
                startActivity(new Intent(this, IndexActivity.class));
                break;
        }
    }
}
